package com.tt.fashionWebSite.dao.jpa;

import com.tt.fashionWebSite.dao.ChiTietKhuyenMaiDao;
import com.tt.fashionWebSite.dao.ChiTietSanPhamDao;
import com.tt.fashionWebSite.dao.HinhAnhSanPhamDao;
import com.tt.fashionWebSite.domain.ChiTietSanPham;
import com.tt.fashionWebSite.domain.SanPham;
import com.tt.fashionWebSite.domain.SanPhamFull;
import com.tt.fashionWebSite.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Repository("chiTietSanPham")
public class ChiTietSanPhamJpaDao extends GenericJpaDao<ChiTietSanPham, String> implements ChiTietSanPhamDao {
   @Autowired
   @Qualifier("chiTietKhuyenMai")
   private ChiTietKhuyenMaiDao chiTietKhuyenMaiDao;

   @Autowired
   @Qualifier("hinhAnhSanPham")
   private HinhAnhSanPhamDao hinhAnhSanPhamDao;

    public ChiTietSanPhamJpaDao(){
        super(ChiTietSanPham.class);
    }

    @Override
    public List<ChiTietSanPham> getAllProductDetail(){
        return getEntityManager().createNamedQuery(ChiTietSanPham.GET_PRODUCTDETAIL_ALL, ChiTietSanPham.class).getResultList();
    }

    @Override
    public List<ChiTietSanPhamFullDto> getAllProductDetailByMaSP(String maSP) {
        String strQuery = "SELECT "
              + "c.MA_CT_SP, "
              + "m.MA_MAU, "
              + "m.TEN_MAU,"
              + "k.MA_SIZE, "
              + "k.TEN_SIZE, "
              + "c.GIA_SP, "
              + "c.SO_LUONG, "
              + "h.MA_HOA_TIET, "
              + "h.TEN_HOA_TIET "
              + "FROM chi_tiet_san_pham c "
              + "JOIN mau_sac m "
               + "ON c.MA_MAU_SAC = m.MA_MAU "
              + "JOIN hoa_tiet h "
              + "ON c.MA_HOA_TIET = h.MA_HOA_TIET "
              + "JOIN kich_thuoc k  "
               + "ON c.MA_SIZE = k.MA_SIZE "
              + "WHERE c.MA_SP = :maSP ";

        Query query = getEntityManager().createNativeQuery(strQuery);
        query.setParameter("maSP", maSP);

        List<Object> results = query.getResultList();
        List<ChiTietSanPhamFullDto> chiTietSanPhamFullDtos = new ArrayList<ChiTietSanPhamFullDto>();
        ChiTietSanPhamFullDto chiTietSanPhamFullDto = null;
        for (int i = 0; i < results.size(); i++) {
           Object[] userDB = (Object[]) results.get(i);
           chiTietSanPhamFullDto = new ChiTietSanPhamFullDto(userDB);
//           get list deal
           List<ChiTietKhuyenMaiFullDto> chiTietKhuyenMaiFullDto = chiTietKhuyenMaiDao.getAllDealDetailByProduct(userDB[0].toString());
           chiTietSanPhamFullDto.setChiTietKhuyenMai(chiTietKhuyenMaiFullDto);
//           get list image
           List<HinhAnhSanPhamDto> hinhAnhSanPhamDtos = hinhAnhSanPhamDao.getImageByProduct(userDB[0].toString());
           chiTietSanPhamFullDto.setHinhAnhSanPham(hinhAnhSanPhamDtos);

           chiTietSanPhamFullDtos.add(chiTietSanPhamFullDto);
        }

        return chiTietSanPhamFullDtos;
    }

   @Override
   public List<ChiTietSanPhamRuleDto> getDetailProductForRule(String maSP) {
      String strQuery = "SELECT "
            + "c.MA_CT_SP, "
            + "c.MA_SP "
            + "FROM chi_tiet_san_pham c "
            + "WHERE c.MA_SP = :maSP ";

      Query query = getEntityManager().createNativeQuery(strQuery);
      query.setParameter("maSP", maSP);

      List<Object> results = query.getResultList();
      List<ChiTietSanPhamRuleDto> chiTietSanPhamFullDtos = new ArrayList<ChiTietSanPhamRuleDto>();
      ChiTietSanPhamRuleDto chiTietSanPhamFullDto = null;
      for (int i = 0; i < results.size(); i++) {
         Object[] userDB = (Object[]) results.get(i);
         chiTietSanPhamFullDto = new ChiTietSanPhamRuleDto(userDB);

         chiTietSanPhamFullDtos.add(chiTietSanPhamFullDto);
      }

      return chiTietSanPhamFullDtos;
   }

    @Override
    public List<ChiTietSanPham> getDataDetailOfDataCommon(String maSP) {
        return getEntityManager().createNamedQuery(ChiTietSanPham.GET_SEARCH_DETAIL_OF_ID, ChiTietSanPham.class)
                .setParameter("maSP", maSP)
                .getResultList();
    }

    @Override
    public void deleteProductDetail(String maCTSP) {
        Query q1 = getEntityManager().createQuery("DELETE FROM ChiTietSanPham c WHERE c.maCTSP = :maCTSP ");
        q1.setParameter("maCTSP", maCTSP);
        q1.executeUpdate();
    }
}
